package dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import dao.Product_CommentDao;
import entity.Product_Comment;
import util.C3P0Util;

public class Product_CommentDaoImpl implements Product_CommentDao {

	//计算总条数
		@Override
		public List<Product_Comment> getAllUserInfo(Integer pageNo, Integer paeSize, HashMap<String, String> map) {
			List<Product_Comment> list = null;
					// 操作数据库对象
					QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
					// 编写SQL语句
					String sql = "select COUNT(comment_id) as number  FROM product_comment where 1=1 ";
					StringBuffer sb=new StringBuffer();
					sb.append(sql);
					//根据map 中的数据进行SQL 拼接
					if (map.get("username")!=null && !map.get("username").trim().equals("")) {
						sb.append(" and username like '%"+map.get("username").trim()+"%'");
					}
					if (map.get("userId")!=null && !map.get("userId").trim().equals("")) {
						sb.append(" and userId = "+map.get("userId").trim());
					}
					sb.append(" limit ?,?");
					System.out.println(sb);
					try {
						list = qr.query(sb.toString(),new Integer[]{(pageNo-1)*paeSize,paeSize},new
								BeanListHandler<Product_Comment>(Product_Comment.class));
					} catch (SQLException e) {
						e.printStackTrace();
					}

					return list;
		}

		
		
		//获取所有对象 做分页展示用 
		@Override
		public Integer getRows(HashMap<String, String> map) {
			  		// 操作数据库对象
					QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
					// 编写SQL语句
					String sql = "select COUNT(comment_id) as number  FROM product_comment where 1=1 ";
					StringBuffer sb=new StringBuffer();
					sb.append(sql);
					//根据map 中的数据进行SQL 拼接
					if (map.get("username")!=null && !map.get("username").trim().equals("")) {
						sb.append(" and username like '%"+map.get("username").trim()+"%'");
					}
					if (map.get("userId")!=null && !map.get("userId").trim().equals("")) {
						sb.append(" and userId = "+map.get("userIdK").trim());
					}
					// 查询结果
					Long i = null;
					try {
						i=(Long)qr.query(sb.toString(), new ScalarHandler("number"));
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}			
					return i.intValue();	
		}

		
		//添加留言
		@Override
		public boolean addComment(Product_Comment pc) {
			// 操作数据库对象
			QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
			String sql ="INSERT INTO `product_comment` VALUES(null,?,?,?,?,?,?,?,?)";
			int i = 0;
			Object[] parames = {pc.getProduct_id(),pc.getOrder_id(),pc.getOrder_id(),pc.getTitle(),pc.getContent(),pc.getAudit_status(),pc.getAudit_time(),pc.getModified_time()};
			
			try {
				
				i = qr.update(sql,parames);
				if(i==1){
					return true;
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			return false;
		}

}
